package com.lxmes.gs.service.impl;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.gs.domain.TaskIssue;
import com.lxmes.gs.service.ITaskIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.CreateTaskMapper;
import com.lxmes.gs.domain.CreateTask;
import com.lxmes.gs.service.ICreateTaskService;

/**
 * 创建任务Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class CreateTaskServiceImpl extends ServiceImpl<CreateTaskMapper,CreateTask> implements ICreateTaskService{
    
    @Autowired
    private CreateTaskMapper createTaskMapper;
    @Autowired
    protected Validator validator;
    @Autowired
    private ITaskIssueService taskIssueService;
    /**
     * 查询创建任务
     * 
     * @param id 创建任务主键
     * @return 创建任务
     */
    @Override
    public CreateTask selectCreateTaskById(Long id){
        return createTaskMapper.selectById(id);
    }

    /**
     * 查询创建任务列表
     * 
     * @param createTask 创建任务
     * @return 创建任务
     */
    @Override
    public List<CreateTask> selectCreateTaskList(CreateTask createTask){
        return createTaskMapper.selectList(Wrappers.lambdaQuery(createTask));
    }

    /**
     * 新增创建任务
     * 
     * @param createTask 创建任务
     * @return 结果
     */
    @Override
    public int insertCreateTask(CreateTask createTask){
        createTask.setCreateTime(DateUtils.getNowDate());
        String taskCode = StrUtil.format("RW{}{}" , DateUtil.format(new Date(), "yyyyMMdd"), NumberUtil.decimalFormat("000" , RandomUtil.randomInt(1, 1000)));
        createTask.setTaskCode(taskCode);
        return createTaskMapper.insert(createTask);
    }

    /**
     * 修改创建任务
     * 
     * @param createTask 创建任务
     * @return 结果
     */
    @Override
    public int updateCreateTask(CreateTask createTask){
        return createTaskMapper.updateById(createTask);
    }

    /**
     * 批量删除创建任务
     * 
     * @param ids 需要删除的创建任务主键
     * @return 结果
     */
    @Override
    public int deleteCreateTaskByIds(Long[] ids){
        return createTaskMapper.deleteCreateTaskByIds(ids);
    }

    /**
     * 删除创建任务信息
     * 
     * @param id 创建任务主键
     * @return 结果
     */
    @Override
    public int deleteCreateTaskById(Long id){
        return createTaskMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<CreateTask> createTaskList, boolean updateSupport) {
        if (StringUtils.isNull(createTaskList) || createTaskList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CreateTask createTask : createTaskList)
        {
            try
            {
                BeanValidators.validateWithException(validator, createTask);
                this.insertCreateTask(createTask);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int auditCreateTask(Long[] ids) {
        List<CreateTask> createTasks = baseMapper.selectBatchIds(Arrays.asList(ids));
        if (createTasks.stream().anyMatch(i -> StrUtil.equals(i.getAudit(), "1"))) {
            throw new ServiceException("存在已审核任务，操作失败");
        }
        for (CreateTask createTask : createTasks) {
            createTask.setAudit("1");
            this.updateCreateTask(createTask);
            TaskIssue taskIssue = BeanUtil.copyProperties(createTask, TaskIssue.class);
            taskIssue.setAudit("0");
            taskIssue.setIssueTime(new Date());
            taskIssueService.insertTaskIssue(taskIssue);
        }
        return 1;
    }
}
